class ToursController < ApplicationController
  layout 'general'
  
  # GET /tours/1
  def show
    @tour = Tour.find(params[:id], :include => :school)
    #@discussions = @tour.discussions.find(:all, :order => 'created_at asc')
    #@discussion = TourComment.new
    respond_to do |format|
      format.html
    end
  end
  
  # GET /tours/new
  def new
    @tour = Tour.new
    #@tour.school = School.find(params[:school_id])
  end
  
  # POST /tours
  def create
    @tour = Tour.new(params[:tour])
    
    # set user who created the tour
    participation = Participation.new
    participation.tour = @tour
    participation.user = current_user
    
    @tour.save! && participation.save!
    respond_to do |format|
      format.html { redirect_to tour_path(@tour) }
    end
  end
  
  # GET /tours/1;edit
  def edit
    @tour = Tour.find(params[:id])
  end
  
  # PUT /tours/1
  def update
    @tour = Tour.find(params[:id])
    @tour.update_attributes!(params[:tour])
    respond_to do |format|
      format.html { redirect_to tour_path(@tour) }
    end
  end
  
  # DELETE /tours/1
  def destroy
    @tour = Tour.find(params[:id])
    @tour.destroy
    respond_to do |format|
      format.html { redirect_to "/jion" }
    end
  end
  
  # PUT /tours/1;join
  def join
    participation = Participation.new
    participation.tour_id = params[:id]
    participation.user = current_user
    participation.save!
    
    respond_to do |format|
      format.html { redirect_to tour_path(params[:id]) }
    end
  end
  
  def quit
    participation = Participation.find(:first, :conditions => ["tour_id=? and user_id=?", params[:id], self.current_user.id])
    participation.destroy
    respond_to do |format|
      format.html { redirect_to tour_path(params[:id]) }
    end
  end
  
private
  def index
  end
end
